<?php



class PFTCoreAPI{
	
	
	// 获取指定产品价格及库存
	// $mode   获取类型 0 分销价 1 零售价 2 都获取
	// $lower  指定日期获取不到价格是否获取最低价
	public static function pStorage($inside, $account, $pid, $aid, $today, $mode=2, $storage=1, $lower=true){
		
		$clsResult = array('js'=>array('p'=>-1,'s'=>-1), 'ls'=>array('p'=>-1,'s'=>-1));// 返回的价格数组
		if($mode==0 || $mode==2){// 获取结算价
			$resxml = $inside->Dynamic_Price_And_Storage($account, $pid, $today, 1, 0, 1, $aid);
			if($resxml==105  && $lower) $resxml = $inside->Dynamic_Price_And_Storage($account, $pid, $today, 2, 0, $storage, $aid);
			if($resxml==105 || $resxml==1065) return $clsResult;
			$simxml = simplexml_load_string($resxml);
			$clsResult['js']['p'] = (string)$simxml->Rec->UUsprice/100;
			if($storage==1) $clsResult['js']['s'] = (string)$simxml->Rec->UUstorage;
		}
		
		if($mode==1 || $mode==2 ){// 获取零售价
			$resxml = $inside->Dynamic_Price_And_Storage($account, $pid, $today, 1, 1, 1, $aid);
			if($resxml==105 && $lower) $resxml = $inside->Dynamic_Price_And_Storage($account, $pid, $today, 2, 1, $storage, $aid);
			if($resxml==105 || $resxml==1065) return $clsResult;
			$simxml = simplexml_load_string($resxml);
			$clsResult['ls']['p'] = (string)$simxml->Rec->UUsprice/100;
			if($storage==1) $clsResult['ls']['s'] = (string)$simxml->Rec->UUstorage;
		}
		return $clsResult;
	}
	
	// 获取产品信息
	public static function salePros($condition = array()){
	
		$prods     = array();
		$memberSID = isset($condition['memberSID']) ? $condition['memberSID']:$_SESSION['sid'];
		
		if(isset($condition['SysFields'])){
			$fields = array('p.id as pid,p.apply_did,p.p_status,l.id as lid,l.p_type,l.jtype,l.areacode,l.title,getPY(l.title,1) as py,l.salerid,l.area,l.topic,l.px,l.runtime,l.taobao_support,l.imgpath,l.address,t.id as tid,t.pay,t.getaddr,t.title as ttitle,t.tprice,t.notes,t.buy_limit_low,t.ddays,t.delaydays,t.status,f.ass_station,f.tourist_info,f.mdays,f.mhour,f.rdays,t.order_end,t.order_start,t.delaytype,t.sync_status');
		}else{
			$fields = array('l.id as lid,t.id as tid,p.id as pid,p.apply_did,l.title as ltitle,t.title as ttitle');
			if(isset($condition['fields'])) $fields = array_merge($fields, $condition['fields']);
		}
		
		$sql = "select ".implode(',', $fields)." from uu_products p left join uu_land l on p.contact_id=l.id left join uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id";
		
		$order = isset($condition['orderby']) ? ' '.$condition['orderby']:' order by l.px desc';
		
		// 条件刷选
		$p_status    = 0;
		$apply_limit = 1;
		if(isset($condition['p_status']))    $p_status    = $condition['p_status'];
		if(isset($condition['apply_limit'])) $apply_limit = $condition['apply_limit'];
		$where = "where p.p_status=$p_status and p.apply_limit=$apply_limit";
		
		if(isset($condition['lid'])) $where.=" and l.id={$condition['lid']} ";
		if(isset($condition['pid'])) $where.=" and p.id={$condition['pid']} ";
		if(isset($condition['tid'])) $where.=" and t.id={$condition['tid']} ";
		
		// 一手供应
		if(isset($condition['NoDirect']) && $condition['NoDirect']) goto Evolute;
		$sql_search = "select l.pids,l.aid from pft_product_sale_list l left join pft_member m on l.aid=m.id where l.fid=$memberSID and l.status=0 and m.status=0";
		$GLOBALS['le']->query($sql_search);
		while($row=$GLOBALS['le']->fetch_assoc()) $pids_a[$row['aid']] = $row['pids'] ? explode(',', $row['pids']):'';
		$pids_a[$memberSID] = array('A');
		$pids_a = array_filter($pids_a);
		$aids_a = array_keys($pids_a);// 获取一手供应商
		if(count($aids_a)){
			$aids_s  = implode(',', $aids_a);
			$where1  = $where." and p.apply_did in ($aids_s)";
			$sql_lv1 = "$sql $where1 $order";
			$GLOBALS['le']->query($sql_lv1);
			while($row=$GLOBALS['le']->fetch_assoc()){
				if($pids_a[$row['apply_did']][0]!='A' && !in_array($row['pid'], $pids_a[$row['apply_did']])) continue;
				$row['aid'] = 0;
				$row['lvl'] = 0;
				$row['active']     = 0;
				$row['sapply_did'] = $row['apply_did'];
				$prods[] = $row;
			}
		}
		
		// 转分销
		Evolute:
		if(isset($condition['NoEvolute']) && $condition['NoEvolute']) goto The_End;
		$str = "select id,pid,sid,active,lvl,status from pft_p_apply_evolute where fid=$memberSID and status=0 and sid<>sourceid and sourceid<>$memberSID";
		echo $str;
		$GLOBALS['le']->query($str);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$secondInfo[$row['pid'].'_'.$row['sid']] = $row;
			$secondPids[] = $row['pid'];
		}
		if(count($secondPids)){
			$proInfo    = array();
			$secondPids = implode(',', $secondPids);
			$where2 = $where." and p.id in ($secondPids) ";
			$str2   = "$sql $where2 $order";
			$GLOBALS['le']->query($str2);
			while($row=$GLOBALS['le']->fetch_assoc()) $proInfo[$row['pid']] = $row;
			foreach($secondInfo as $key=>$value){
				list($pid, $sapply_did) = explode('_', $key);
				if(!isset($proInfo[$pid])) continue;
				$row = $proInfo[$pid];
				$row['aid'] = $sapply_did;
				$row['lvl'] = $value['lvl'];
				$row['eid'] = $value['id'];
				$row['active']     = $value['active'];
				$row['estatus']    = $value['status'];
				$row['sapply_did'] = $sapply_did;
				$prods[] = $row;
			}
		}
		
		The_End:
		return $prods;
	}
	
	
	// 获取会员的供应商列表
	public static function Suppliers($memberID){
		$suppliers = array();
		$sql = "select r.id,r.parent_id,m.dname from pft_member_relationship r left join pft_member m on r.parent_id=m.id where r.son_id=$memberID and r.ship_type=0 and r.status=0 and parent_id<>$memberID and m.status=0 and m.id>1";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $suppliers[$row['parent_id']] = $row['dname'];
		return $suppliers;
	}
	
	// 获取会员的分销商列表
	public static function Distributors($memberID){
		$distributors = array();
		$sql = "select r.id,r.son_id,m.dname from pft_member_relationship r left join pft_member m on r.son_id=m.id where r.parent_id=$memberID and r.ship_type=0 and r.status=0 and son_id<>$memberID and m.status<3 and m.id>1";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $distributors[$row['son_id']] = $row['dname'];
		return $distributors;
	}
	
}